package top.qitun.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;

import io.swagger.annotations.ApiOperation;
import top.qitun.bizz.account.tuser.service.TUserService;
import top.qitun.bizz.user.request.modal.RegisterRequest;
import top.qitun.bizz.user.tuser.entity.TUserBO;
import top.qitun.model.response.ObjectDataResponse;
import top.qitun.util.OrikaMapper;

/**
 * @author Zhao Junjian
 */
@RestController
@RequestMapping(value = "/v1", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class UserController {
	Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
    @Autowired
    private TUserService userService;

//    @Delay
//    @RandomlyThrowsException
//    @ApiOperation(value = "根据ID获取用户", notes = "")
//    @RequestMapping(value = "/users/{userId}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    public ObjectDataResponse<User> findUser(@PathVariable Long userId) {
//        final User user = userService.find(userId);
//        if (user == null) {
//            Shift.fatal(StatusCode.USER_NOT_EXISTS);
//        }
//        return new ObjectDataResponse<User>(user);
//    }

//    @Delay
//    @RandomlyThrowsException
//    @ApiOperation(value = "获取全部用户", notes = "")
//    @RequestMapping(value = "/users", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    public ObjectCollectionResponse<User> findAll() {
//        final List<User> userList = userService.findAll(0, 10000);
//        return new ObjectCollectionResponse<>(userList);
//    }

    @ApiOperation(value = "用户注册", notes = "注册新用户, 余额自定义, 用于下单等一系列操作, 并可获取JWT")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ObjectDataResponse<TUserBO> register(@Valid @RequestBody RegisterRequest request, BindingResult error) {
    	String desc = "用户注册";
        try {
        	Preconditions.checkNotNull(request);
            final TUserBO dbUser = userService.find(request.getMobile());
            if (dbUser != null) {
            	logger.warn("用户已注册!");
            	return new ObjectDataResponse<TUserBO>(1,desc+"失败,用户已存在!");
            }
            // 重新计算密码
            TUserBO transientUser = OrikaMapper.map(request, TUserBO.class);
            transientUser = userService.register(transientUser);
//            RegisterResponse returnObject = OrikaMapper.map(transientUser, RegisterResponse.class);
            return new ObjectDataResponse<TUserBO>(0,desc+"用户成功!",transientUser);
		} catch (Exception e) {
			logger.error("用户注册失败",e);
			return new ObjectDataResponse<TUserBO>(-1,"系统错误!"+e.getMessage());
		}
    }
    
    @ApiOperation(value = "查询用户", notes = "根据手机号查询用户信息")
    @RequestMapping(value = "/user/m/{mobile}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
    public ObjectDataResponse<TUserBO> queryUserbyMobile(@PathVariable("mobile") String mobile) {
    	String desc = "查询用户";
        try {
            final TUserBO dbUser = userService.queryByMobile(mobile);
            if (dbUser == null) {
            	return new ObjectDataResponse<TUserBO>(1,desc+"用户不存在!");
            }
            return new ObjectDataResponse<TUserBO>(0,desc+"用户成功!",dbUser);
		} catch (Exception e) {
			logger.error("用户注册失败",e);
			return new ObjectDataResponse<TUserBO>(-1,"系统错误!"+e.getMessage());
		}
    }

//    @Delay
//    @RandomlyThrowsException
//    @ApiOperation(value = "用户余额变更", notes = "直接变更指定用户的余额")
//    @RequestMapping(value = "/users/{userId}/balance", method = RequestMethod.PATCH)
//    public ObjectDataResponse<User> recharge(@PathVariable Long userId, @Valid @RequestBody RechargeRequest request, BindingResult error) {
//        final User user = userService.find(userId);
//        if (user == null) {
//            Shift.fatal(StatusCode.USER_NOT_EXISTS);
//        }
//        user.setBalance(request.getAmount());
//        userService.updateNonNullProperties(user);
//        return new ObjectDataResponse<>(user);
//    }

}
